<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //原型继承
        function Animal (){
            this.sex = 'man'
            this.eat = function (){
                console.log ('eat')
            }
        }
        Animal.prototype = {aaa:100}
        function Humen (name,age) {
            this.name = name
            this.age = age

        }
        Humen.prototype = new Animal()
        var xiaoming = new Humen('小明',22)
        var xiaohong = new Humen('小红',20)
        xiaohong.__proto__.sex='woman'
        console.log(xiaohong)
        console.log(xiaoming)
        //优点：1.可以继承原型上的所有属性 2.可以继承原型的原型......上的所有属性和方法
        //缺点：一个实例改变其原型上的属性，其他的所有实例都会跟着更改

        
        //构造函数继承
        function Animal (){
            this.sex = 'man'
            this.eat = function (){
                console.log ('eat')
            }
        }
        Animal.prototype = {aaa:100}
        function Humen (name,age) {
            //{sex:'man',eat:'function',name,age}
            this.name = name
            this.age = age
            Animal.call(this)

        }
        var xiaoming = new Humen('小明',22)
        var xiaohong = new Humen('小红',20)
        xiaohong.__proto__.sex='woman'
        console.log(xiaohong)
        console.log(xiaoming)
        //优点：一个实例修改原型的属性，不会影响其他的实例
        //缺点：获取不到原型的原型上的属性和方法
    
        //组合继承
        function Animal (){
            this.sex = 'man'
            this.eat = function (){
                console.log ('eat')
            }
        }
        Animal.prototype = {aaa:100}
        function Humen (name,age) {
            //{sex:'man',eat:'function',name,age}
            this.name = name
            this.age = age
            Animal.call(this)

        }
       Humen.prototype = new Animal()

       //寄生组合继承
       function Animal (){
            this.sex = 'man'
            this.eat = function (){
                console.log ('eat')
            }
        }
        Animal.prototype = {aaa:100}
        var obj = Object.create(Animal.prototype)
        function Humen (name,age) {
            //{sex:'man',eat:'function',name,age}
            this.name = name
            this.age = age
            Animal.call(this)

        }
       Humen.prototype =obj
       //特性 1.一个实例改变原型上的属性，其他实例不会改变
      // 2.可以获取到原型上的原型的属性和方法
      // 3.Animal函数少运行一次，减少了开销

    </script>    
</body>
</html>